package com.yilin.tms.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.entity.system.system.Jurisdiction;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.account.Authority;
import com.yilin.tms.user.mapper.AuthorityMapper;
import com.yilin.tms.user.service.IAuthorityervice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 认证服务
 *
 * @author zhangjinyuan
 * @date 2022/10/26
 */
@Service
@Transactional
public class AuthorityService extends ServiceImpl<AuthorityMapper, Authority> implements IAuthorityervice {
    @Resource
    private  AuthorityMapper authorityMapper;
    @Override
    public void   saveAuthority(Account account, List<Jurisdiction> jurisdictionList){
        deleteAuthorityListByAccountId(account.getId());
        List<Authority> authorityList=new ArrayList<>();
        for (Jurisdiction jurisdiction : jurisdictionList) {
            Authority authority = new Authority();
            authority.setOrganizeId(account.getOrganizeId());
            authority.setOrganizeName(account.getOrganizeName());
            authority.setAccountId(account.getId());
            authority.setAccountName(account.getName());
            authority.setCode(jurisdiction.getCode());
            authority.setRemark(jurisdiction.getRemark());
            authorityList.add(authority);
        }
        saveBatch(authorityList);
    }
    @Override
    public List<Authority>   getAuthorityListByyAccountId(String accountId){
         QueryWrapper<Authority> queryWrapper=new QueryWrapper<>();
         queryWrapper.eq("account_id",accountId);
         return  authorityMapper.selectList(queryWrapper);
    }
    @Override
    public  void deleteAuthorityListByAccountId(String accountId){
        List<Authority> list=getAuthorityListByyAccountId(accountId);
        if(list!=null&& !list.isEmpty()){
            authorityMapper.deleteBatchIds(list);
        }
    }



}
